package com.example.ccj.b.c.custom.tree.tree2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.ccj.R;
import com.example.ccj.b.c.custom.tree.TreeMode;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.SizeUtils;
import com.example.ccj.u.StringUtils;

import java.util.List;

/**
 * 显示选择的人员
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/11/22 0:37
 */
public class TreeStructureSelectTreeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private final List<TreeMode> treeModeList;
    private ITreeResultListener iTreeResultListener;
    private boolean isCnSelectBuMen = true;
    private boolean isCanSelectRenYuan = true;
    private boolean isShowSuoShuJieDian = false;

    static RequestOptions requestOptions = RequestOptions
            .bitmapTransform(new RoundedCorners(SizeUtils.dp2px(10)));

    /**
     *
     * @param treeModeList 数据
     * @param iTreeResultListener 回调
     */
    public TreeStructureSelectTreeAdapter(List<TreeMode> treeModeList, ITreeResultListener iTreeResultListener){
        this.treeModeList = treeModeList;
        this.iTreeResultListener = iTreeResultListener;
    }

    /**
     *
     * @param treeModeList 数据
     * @param iTreeResultListener 回调
     */
    public TreeStructureSelectTreeAdapter(List<TreeMode> treeModeList, ITreeResultListener iTreeResultListener, boolean isCnSelectBuMen){
        this.treeModeList = treeModeList;
        this.iTreeResultListener = iTreeResultListener;
        this.isCnSelectBuMen = isCnSelectBuMen;
    }

    /**
     *
     * @param treeModeList 数据
     * @param iTreeResultListener 回调
     * @param isCnSelectBuMen 部门是否可选择
     * @param isShowSuoShuJieDian 是否否显示节点所属上级节点
     */
    public TreeStructureSelectTreeAdapter(List<TreeMode> treeModeList, ITreeResultListener iTreeResultListener, boolean isCnSelectBuMen, boolean isShowSuoShuJieDian){
        this.treeModeList = treeModeList;
        this.iTreeResultListener = iTreeResultListener;
        this.isCnSelectBuMen = isCnSelectBuMen;
        this.isShowSuoShuJieDian = isShowSuoShuJieDian;
    }

    /**
     *
     * @param treeModeList 数据
     * @param iTreeResultListener 回调
     * @param isCanSelectRenYuan 人员是否可选择
     * @param isCnSelectBuMen 部门是否可选择
     * @param isShowSuoShuJieDian 是否否显示节点所属上级节点
     */
    public TreeStructureSelectTreeAdapter(List<TreeMode> treeModeList, ITreeResultListener iTreeResultListener,boolean isCanSelectRenYuan, boolean isCnSelectBuMen, boolean isShowSuoShuJieDian){
        this.treeModeList = treeModeList;
        this.iTreeResultListener = iTreeResultListener;
        this.isCanSelectRenYuan = isCanSelectRenYuan;
        this.isCnSelectBuMen = isCnSelectBuMen;
        this.isShowSuoShuJieDian = isShowSuoShuJieDian;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == MULU) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_tree_structure_select, parent, false);
            return new ViewHolder1(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_tree_structure2, parent, false);
            return new ViewHolder2(view);
        }
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TreeMode treeMode = treeModeList.get(position);
        //部门级别
        if (holder instanceof ViewHolder1) {
            ViewHolder1 viewHolder = (ViewHolder1) holder;
            viewHolder.name.setText(treeMode.name);

            if (!StringUtils.isEmpty(treeMode.buMenStr) && isShowSuoShuJieDian) {
                viewHolder.jiedian.setVisibility(View.VISIBLE);
                viewHolder.jiedian.setText(treeMode.buMenStr);
            } else {
                viewHolder.jiedian.setVisibility(View.GONE);
                viewHolder.jiedian.setText("");
            }

            if (treeMode.isChecked) {
                ImageUtils.loadImage(viewHolder.select.getContext(),R.drawable.tree_checked,viewHolder.select);
            } else {
                ImageUtils.loadImage(viewHolder.select.getContext(),R.drawable.tree_no_check,viewHolder.select);
            }

            //选择
            if (isCnSelectBuMen) {
                viewHolder.infoLL.setOnClickListener(v -> {
                    treeMode.isChecked = !treeMode.isChecked;
                    if (iTreeResultListener != null) iTreeResultListener.onSelectChange(treeMode,treeMode.isChecked);
                    notifyItemChanged(position);
                });
            } else {
                viewHolder.select.setVisibility(View.GONE);
            }
        //人员级别
        } else if (holder instanceof ViewHolder2) {
            ViewHolder2 viewHolder = (ViewHolder2) holder;
            viewHolder.name.setText(treeMode.name);

            //上层节点名称显示
            if (!StringUtils.isEmpty(treeMode.buMenStr) && isShowSuoShuJieDian) {
                viewHolder.jiedian.setVisibility(View.VISIBLE);
                viewHolder.jiedian.setText(treeMode.buMenStr);
            } else {
                viewHolder.jiedian.setVisibility(View.GONE);
                viewHolder.jiedian.setText("");
            }

            if (treeMode.isChecked) {
                ImageUtils.loadImage(viewHolder.select.getContext(),R.drawable.tree_checked,viewHolder.select);
            } else {
                ImageUtils.loadImage(viewHolder.select.getContext(),R.drawable.tree_no_check,viewHolder.select);
            }

            if (!StringUtils.isEmpty(treeMode.icon)) {
                ImageUtils.loadImage(viewHolder.icon.getContext(),treeMode.icon,viewHolder.icon);
            }

            //选择
            if (isCanSelectRenYuan) {
                viewHolder.infoLL.setOnClickListener(v -> {
                    treeMode.isChecked = !treeMode.isChecked;
                    if (iTreeResultListener != null) iTreeResultListener.onSelectChange(treeMode,treeMode.isChecked);
                    notifyRemove(position);
                });
            } else {
                viewHolder.select.setVisibility(View.GONE);
            }
        }
    }


    /**
     * 获取当前节点下的人员数量
     * @param treeMode 选中的节点
     */
    private int getSelectedAllData(TreeMode treeMode,int i){
        if (treeMode != null) {
            if (treeMode.type == TreeMode.DATA_TYPE_MU_LU)  {
                List<TreeMode> treeModeList = treeMode.treeModeList;
                if (!treeModeList.isEmpty()) {
                    if (treeModeList.get(0).type != TreeMode.DATA_TYPE_MU_LU) i += treeModeList.size();
                    else {
                        for (TreeMode mode : treeModeList) {
                            i += getSelectedAllData(mode,0);
                        }
                    }
                }
            } else if (treeMode.type == TreeMode.DATA_TYPE_DATA) {
                i ++;
            }
        }
        return i;
    }


    @Override
    public int getItemCount() {
        return treeModeList == null ? 0 : treeModeList.size();
    }


    private  int MULU = 0;
    private  int DATA = 1;


    /**
     * 删除节点
     * @param position
     */
    private void notifyRemove(int position){
        TreeMode remove = treeModeList.remove(position);
        notifyItemRemoved(position);
        //重新加载从删除位置往下的所有item，如果删除的是中间，自删除的位置到最后开始是做重新添加操作
        //避免下表越界等错误
        notifyItemRangeRemoved(position,getItemCount());
    }



    @Override
    public int getItemViewType(int position) {
        TreeMode treeMode = treeModeList.get(position);
        if (treeMode.type == TreeMode.DATA_TYPE_MU_LU) {
            return MULU;
        } else if (treeMode.type == TreeMode.DATA_TYPE_DATA) {
            return DATA;
        }
        return 0;
    }

    //
    public static class ViewHolder1 extends RecyclerView.ViewHolder{
        private final TextView name,jiedian;
        private final LinearLayout infoLL;
        private final ImageView select;
        public ViewHolder1(@NonNull View itemView) {
            super(itemView);
            infoLL = itemView.findViewById(R.id.adapter_tree_structure_info_ll1);
            select = itemView.findViewById(R.id.adapter_tree_structure_select1);
            name = itemView.findViewById(R.id.adapter_tree_structure_name1);
            jiedian = itemView.findViewById(R.id.adapter_tree_structure_jie_dian);
        }
    }

    public static class ViewHolder2 extends RecyclerView.ViewHolder{
        private final TextView name,jiedian;
        private final ImageView icon;
        private final LinearLayout infoLL;
        private final ImageView select;
        public ViewHolder2(@NonNull View itemView) {
            super(itemView);
            infoLL = itemView.findViewById(R.id.adapter_tree_structure_info_ll);
            icon = itemView.findViewById(R.id.adapter_tree_structure_icon);
            select = itemView.findViewById(R.id.adapter_tree_structure_select);
            name = itemView.findViewById(R.id.adapter_tree_structure_name);
            jiedian = itemView.findViewById(R.id.adapter_tree_structure_jie_dian);
        }
    }




}
